// 重写数组函数
// 获取原来的数组方法
let oladArrayProtoMethods = Array.prototype;
// 继承
export let arrayMethods = Object.create(oladArrayProtoMethods);
// 劫持数组的方法
let methods = [
  'push',
  'pop',
  'unshift',
  'shift',
  'splice'
];

methods.forEach(item => {
  arrayMethods[item] = function (...args) {
    let result = oladArrayProtoMethods[item].apply(this, args);
    // 数组追加对象的劫持
    let inserted;
    switch (item) {
      case 'push':
      case 'unshift':
        inserted = args;
        break;
      case 'splice':
        inserted = args.splice(2);
        break;
    }

    let ob = this.__ob__;
    // 数据新增的值也要进行观测
    if (inserted) {
      ob.observeArray(inserted);
    }

    // 通知数组更新
    ob.dep.notify();
    return result;
  }
})

